﻿#region Copyrights Notice
//©2010 Microsoft Corporation. All rights reserved.
#endregion

#region usings
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;
using Microsoft.Deployment.WindowsInstaller;
#endregion


namespace SetupCustomActionsLibrary
{
    public class UpdateConfigCA
    {
        static string enpointXPath = @"system.serviceModel/client/endpoint";
        static string connectionString = "Data Source = {0};Initial Catalog = {1};Integrated Security = SSPI;Connect Timeout=200";
        /// <summary>
        /// This function updates the PortalService web.config file with Database name and server name
        /// </summary>
        /// <param name="xiSession"></param>
        /// <returns></returns>
        [CustomAction]
        public static ActionResult UpdatePortalConfigFile(Session xiSession/*string webConfigFile, string dbServer, string dbName*/)
        {
            //get the paramaters passed through the CustomAction
            string[] caData = GetSqlServerAndDatabase(xiSession);
            XmlDocument configDoc = new XmlDocument();
            configDoc.Load(caData[0] + "web.config");
            //do and Xpath query to get the existing connection string
            XmlNode connectionStringNode = configDoc.DocumentElement.SelectSingleNode("connectionStrings");
            XmlNode oldAdd = connectionStringNode.SelectSingleNode("add");
            string oldConnString = oldAdd.Attributes.GetNamedItem("connectionString").Value;   
            //form the new connection string with the database serevr and database name taken from the text box
            string connection = string.Format(connectionString, caData[1], caData[2]);  

            oldAdd.Attributes["connectionString"].Value  = connection;
            configDoc.Save(caData[0] + "web.config");

            return ActionResult.Success; 
        }
        /// <summary>
        /// This function updates the WEbsite web.config file with the WCF service endpoint addresses
        /// </summary>
        /// <param name="xiSession"></param>
        /// <returns></returns>
        [CustomAction]
        public static ActionResult UpdateWebSiteConfigFile(Session xiSession)
        {
            //get the passed in arguments
            string[] caData = GetSqlServerAndDatabase(xiSession);
            XmlDocument configDoc = new XmlDocument();
            configDoc.Load(caData[0] + "web.config");
            
            XmlNodeList serviceEndpoints = configDoc.DocumentElement.SelectNodes(enpointXPath);

            foreach (XmlNode node in serviceEndpoints)
            {
                if (node.Attributes["address"].Value.Contains("AuthZ"))
                {
                    //repalce the node with the one entered in the textbox
                    node.Attributes["address"].Value = caData[1];
                }
                else if (node.Attributes["address"].Value.Contains("Portal"))
                { 
                    node.Attributes["address"].Value = caData[2]; 
                }
            }
            configDoc.Save(caData[0] + "web.config");
            return ActionResult.Success;  
        }
        /// <summary>
        /// Parse and return the in argumetns
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
        private static string[] GetSqlServerAndDatabase(Session session)
        {
            string[] keys = new string[3];
            session.CustomActionData.Keys.CopyTo(keys, 0);
            return keys[0].Split(',');
        } 
    }
}
